package com.czz.manage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.czz.manage.entity.Course;
import com.czz.manage.entity.Order;
import com.czz.manage.mapper.*;
import com.czz.manage.service.DashBoardService;
import com.czz.manage.vo.ChartVo;
import com.czz.manage.vo.DashBoardVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: DashBoardServiceImpl
 * @Author Czz
 * @Date 2020/2/18
 * @Time 13:26
 **/
@Service
public class DashBoardServiceImpl implements DashBoardService {

    @Autowired
    UserMapper userMapper;

    @Autowired
    TeacherMapper teacherMapper;

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    CourseMapper courseMapper;

    @Autowired
    CourseConsultMapper consultMapper;

    @Override
    public DashBoardVo getAdminDashBoardData() {
        DashBoardVo dashBoardVo = new DashBoardVo();
        dashBoardVo.setUser(userMapper.selectCount(null));
        dashBoardVo.setTeacher(teacherMapper.selectCount(null));
        dashBoardVo.setOrder(orderMapper.selectCount(null));
        dashBoardVo.setCourse(courseMapper.selectCount(null));
        return dashBoardVo;
    }

    @Override
    public Map<String, Object> getAdminChartData() {
        Map<String, Object> map = new HashMap<>();

        List<ChartVo> courseData = courseMapper.getWeekCourseAdd();
        List<ChartVo> userData = userMapper.getWeekUserAdd();
        List<ChartVo> teacherData = teacherMapper.getWeekTeacherAdd();
        List<ChartVo> orderData = orderMapper.getWeekOrderAdd();

        List<String> dateList = new ArrayList<>();
        List<String> courseList = new ArrayList<>();
        List<String> userList = new ArrayList<>();
        List<String> teacherList = new ArrayList<>();
        List<String> orderList = new ArrayList<>();


        courseData.forEach(course -> {
            dateList.add(course.getDate());
            courseList.add(course.getCount());
        });

        userData.forEach(user -> {
            userList.add(user.getCount());
        });

        teacherData.forEach(teacher -> {
            teacherList.add(teacher.getCount());
        });

        orderData.forEach(order -> {
            orderList.add(order.getCount());
        });

        map.put("dateList",dateList);
        map.put("courseList",courseList);
        map.put("userList",userList);
        map.put("teacherList",teacherList);
        map.put("orderList",orderList);
        return map;
    }

    @Override
    public DashBoardVo getTeacherDashBoardData(String id) {
        Integer course = courseMapper.selectCount(new QueryWrapper<Course>().eq("teacher_id",id));
        Integer consult = consultMapper.countTeacherConsult(id);
        Integer order = orderMapper.selectCount(new QueryWrapper<Order>().eq("teacher_id",id));
        Integer money = orderMapper.countSumTeacherMoney(id);

        DashBoardVo dashBoardVo = new DashBoardVo();
        dashBoardVo.setCourse(course);
        dashBoardVo.setOrder(order);
        dashBoardVo.setMoney(money);
        dashBoardVo.setConsult(consult);

        return dashBoardVo;
    }

    @Override
    public Map<String, Object> getTeacherChartData(String id) {
        Map<String, Object> map = new HashMap<>();
        List<ChartVo> consultData = consultMapper.getTeacherConsultWeek(id);
        List<ChartVo> orderData = orderMapper.getTeacherOrderWeek(id);
        List<ChartVo> moneyData = orderMapper.getTeacherMoneyWeek(id);

        List<String> dateList = new ArrayList<>();
        List<String> consultList = new ArrayList<>();
        List<String> orderList = new ArrayList<>();
        List<String> moneyList = new ArrayList<>();

        orderData.forEach(order -> {
            dateList.add(order.getDate());
            orderList.add(order.getCount());
        });

        consultData.forEach(consult -> {
            consultList.add(consult.getCount());
        });

        moneyData.forEach(money -> {
            moneyList.add(money.getCount());
        });

        map.put("dateList",dateList);
        map.put("consultList",consultList);
        map.put("orderList",orderList);
        map.put("moneyList",moneyList);

        return map;
    }
}
